package com.yky.utils.base;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日期格式转换
 * 
 * @author Administrator
 * 
 */
public class DateUtils {
	
	public static void main(String[] args) {
//		getWeekDateStartAndEnd("2014-04-13");
		
		System.out.println(getDateNoStyle().substring(0,8));
	}
	
	//获得当前日期所在周的起始日期和终止日期
	public static String getWeekDateStartAndEnd(String d) {
		// 得到前多少天
		Calendar cal = Calendar.getInstance();
		cal.setTime(getDate(d));
		// 周一=1 周二=2 周六=6 周日=0
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		System.out.println(w);
		String start = "";
		String end = "";
		if(w==0) {
			start = d;
			end = getAfterDayStyle_point(d, 6);
		}else if(w==6){
			start = getBeforeDayStyle_point(d, w);
			end = d;
		}else{
			start = getBeforeDayStyle_point(d, w);
			end = getAfterDayStyle_point(d, 6-w);
		}
		System.out.println("start=" + start);
		System.out.println("end=" + end);
		return start + ";" + end;
	}
	
	// 获取当前日期是星期几
	public static String getWeekOfDate(Date dt) {
		String[] weekDays = { "日", "一", "二", "三", "四", "五", "六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);

		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;

		return weekDays[w];
	}

	public static Date getDate() {
		Date dateU = new Date();
		java.sql.Date date = new java.sql.Date(dateU.getTime());
		return date;
	}

	public static Date getDate(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = null;
		try {
			d = sdf.parse(date);
		} catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return d;
	}
	
	public static Date getDateHHmmss(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = null;
		try {
			d = sdf.parse(getDateNoStyle_2() + " " + date);
		} catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return d;
	}

	public static String getDate(String date, String format) {
		try {
			if (format == null)
				format = "yyyy-MM-dd HH:mm:ss";
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.format((Date) sdf.parse(date));
		} catch (Exception e) {
			return date;
		}

	}

	public static String getCutStr(String date) {
		return null;
	}

	public static String getDateTime() {
		SimpleDateFormat sdf;
		Date date = null;
		Calendar myDate = Calendar.getInstance();
		myDate.setTime(new Date());
		date = myDate.getTime();
		sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}

	public static String getDateNoStyle() {
		SimpleDateFormat sdf;
		Date date = null;
		Calendar myDate = Calendar.getInstance();
		myDate.setTime(new Date());
		date = myDate.getTime();
		sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(date);
	}

	public static String getDateNoStyle_2() {
		SimpleDateFormat sdf;
		Date date = null;
		Calendar myDate = Calendar.getInstance();
		myDate.setTime(new Date());
		date = myDate.getTime();
		sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}

	public static String formatDate(Date date, String format) {
		if (format == null)
			format = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

//	public static void main(String[] args) {
//		// System.out.println(getDateNoStyle());// 2009-04-12
//		// System.out.println(getDateTime());// 2009-04-12 15:14:37
//		//
////		UUID uuid = UUID.randomUUID();
////		String str = uuid.toString();
////		System.out.println(str);
////		DateUtils.getDateRange("2014-01-25", "2014-03-25");
//	}
	
	public static String getBeforeDayStyle_point(String currentDate, Integer day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getDate(currentDate));
		calendar.add(Calendar.DATE, -day); // 得到前*天
		Date date = calendar.getTime();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}
	
	public static String getAfterDayStyle_point(String currentDate, Integer day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getDate(currentDate));
		calendar.add(Calendar.DATE, day); // 得到后*天
		Date date = calendar.getTime();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}

	public static List<String> getDateRange(String startdate, String enddate) {
		// TODO Auto-generated method stub
		String startdate_before_1 = getBeforeDayStyle_point(startdate, 1);
		List<String> list = new ArrayList<String>();

		Calendar start = Calendar.getInstance();
		start.setTime(getDate(startdate_before_1));

		Calendar end = Calendar.getInstance();
		end.setTime(getDate(enddate));

		while (end.getTime().after(start.getTime())) {
			// 根据日历的规则，为给定的日历字段添加或减去指定的时间量
			start.add(Calendar.DAY_OF_MONTH, 1);
			list.add(formatDate(start.getTime(), "yyyy-MM-dd"));
//			System.out.println(formatDate(start.getTime(), "yyyy-MM-dd"));
		}
		return list;
	}

}
